package com.dsteiner.widgets.client.controller;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.Widgets.IImage;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.model.PrintableImageModel;
import com.dsteiner.widgets.client.model.RateItModel;
import com.dsteiner.widgets.client.model.StarModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class StarController {

	private StarModel starModel;
	private AbstractImagePrototype normalImg;
	private AbstractImagePrototype selectedImg;
	private RateItModel rateItModel;
	private AbstractImagePrototype rate1Img;
	private AbstractImagePrototype rate5Img;
	private AbstractImagePrototype rate4Img;
	private AbstractImagePrototype rate3Img;
	private AbstractImagePrototype rate2Img;
	private ILazyWidget star;
	private IImage image;

	public void registerImage(IImage image) {
        this.image = image;
		image.addMouseOutHandler(new MouseOutHandler(){
			public void onMouseOut(MouseOutEvent event) {
				rateItModel.setSelection(null);
			}
        });
        image.addMouseOverHandler(new MouseOverHandler(){
			public void onMouseOver(MouseOverEvent event) {
				rateItModel.setSelection(starModel.getRate());
			}
        });
        image.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				Integer rating = starModel.getRate();
//				if(rateItModel.getRating() != null && rateItModel.getRating() == starModel.getRate())
//					rating = null;
				rateItModel.setRating(rating);
				rateItModel.setClickedRating(rating);
				rateItModel.setSelection(null);
			}
        });
	}

	public void registerNormalImage(AbstractImagePrototype normalImg) {
		this.normalImg = normalImg;
	}
	public void registerRate1Image(AbstractImagePrototype rate1Img2) {
		this.rate1Img = rate1Img2;
	}
	public void registerRate2Image(AbstractImagePrototype rate2Img) {
		this.rate2Img = rate2Img;
	}
	public void registerRate3Image(AbstractImagePrototype rate3Img) {
		this.rate3Img = rate3Img;
	}
	public void registerRate4Image(AbstractImagePrototype rate4Img) {
		this.rate4Img = rate4Img;
	}
	public void registerRate5Image(AbstractImagePrototype rate5Img) {
		this.rate5Img = rate5Img;
	}

	public void registerRateItModel(RateItModel rateItModel) {
		this.rateItModel = rateItModel;
		rateItModel.addPropertyChangeListener("rating", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateStarStatus();
			}
		});
		rateItModel.addPropertyChangeListener("selection", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateStarStatus();
			}
		});
	}

	public void registerSelectedImage(AbstractImagePrototype selectedImg) {
		this.selectedImg = selectedImg;
	}

	public void registerStar(ILazyWidget star) {
		this.star = star;
		star.addWidgetListener(new WidgetListenerAdapter(){
			@Override
			public void onAttach(IWidget widget) {
				updateStarStatus();
			}
		});
	}

	public void registerStarModel(StarModel starModel) {
		this.starModel = starModel;
	}

	private void updateStarStatus() {
		Integer selection = rateItModel.getSelection();
		Integer rating = rateItModel.getRating();
		int rate = starModel.getRate();
		AbstractImagePrototype imgPrototype = normalImg;
		if(selection == null && rating != null && rating == 1 && rate <= rating){
			imgPrototype = rate1Img;
		}
		else if(selection == null && rating != null && rating == 2 && rate <= rating){
			imgPrototype = rate2Img;
		}
		else if(selection == null && rating != null && rating == 3 && rate <= rating){
			imgPrototype = rate3Img;
		}
		else if(selection == null && rating != null && rating == 4 && rate <= rating){
			imgPrototype = rate4Img;
		}
		else if(selection == null && rating != null && rating == 5 && rate <= rating){
			imgPrototype = rate5Img;
		}
		else if(selection != null && rate <= selection){
			imgPrototype = selectedImg;
		}
		imgPrototype.applyTo(image.getImage());
	}
}
